﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UPos.Core.Entity;
using UPos.Core.RecordProvider;
using UPos.Core.SaleProvider;

namespace UPos.Core
{
    public static class Serv
    {
        static ShopInfo _currentShop;
        static ShopInfo CurrentShop { get { if (_currentShop == null) _currentShop = new ShopInfo(); return _currentShop; } }

        public static void UploadLastDay()
        {
            Upload(DateTime.Now.AddDays(-1));
        }

        public static void Upload()
        {
            Upload(DateTime.Now);
        }

        static void Upload(DateTime date)
        {
            PosSales s = new PosSales(
                date,
                CurrentShop,
                new RecordSQLite());
            if (s.NeedUp)
            {
                var l = s.GetNeedUp();
                var ul = new List<Sale>();
                Services serv = new Services();
                string mess;
                foreach (var item in l)
                {
                    if (serv.CallWebService(item, s.Shop, out mess))
                        ul.Add(item);
                }
                s.Recorder.SaveUnUp(ul, DateTime.Now);
            }
        }

        public static void TestUpload(DateTime date)
        {
            EASSales s = new EASSales(
                date,
                RecordSQLite.GetShop(3),
                new RecordSQLite());

            var l = s.GetNeedUp();
            var ul = new List<Sale>();
            Services serv = new Services();
            string mess;
            foreach (var item in l)
            {
                if (serv.CallWebService(item, s.Shop, out mess))
                    ul.Add(item);
            }
            s.Recorder.SaveUnUp(ul, DateTime.Now);
        }

        
    }
}
